package myself.study.network.udp.demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.junit.Test;

/**
* @Name: UdpClient
* @Description: udp数据报文请求客户端
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-12 上午9:33:18 
* @Version: V1.0
 */
public class UdpClient {
	
	@Test
	public void sendUdpPacket() throws IOException {
		//1、创建发送数据的Socket对象
		DatagramSocket client = new DatagramSocket() ;
		//2、创建数据，并打包
		String sendData = "hello,udp,我来了！" ;
		byte[] buf = sendData.getBytes() ;
		DatagramPacket packet = new DatagramPacket(buf, 
												   buf.length, 
												   InetAddress.getByName("127.0.0.1"), 
												   12345) ;
		//3、发送数据包
		client.send(packet) ;
 		//4、释放资源
		client.close() ;
	}

}
